using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;

namespace Miner_Yedion
{
    public enum Operation { O_Login,O_Grades, O_Notebooks, O_Notebooks_Download };
    public delegate void MyMethod();

    public class ThreadEvents : BackgroundWorker
    {
        public string param1;
        public string param2;
        public string param3;
        public string param4;
        public Boolean Running;
        private System.Windows.Forms.Timer MyTimer;
        private Boolean UseTimerField;
        public Operation Command;
        private MyMethod ReturnMethod;

        public rootSubject[] Result_rootSubject;
        public rootNotebook[] Result_rootNotebook;
        

        private static ThreadEvents instance = null;
        private frmMain MainFormField = null;
        private frmLogin LoginFormField = null;

        public Boolean UseTimer
        {
            get 
            {
                return this.UseTimerField;
            }
            set
            {
                UseTimerField = value;
            }
        
        }

        public frmLogin LoginForm
        {
            get 
            {
                return LoginFormField;
            }
            set 
            {
                this.LoginFormField = value;
            }
        }

        public frmMain MainForm 
        {
            get 
            {
                return MainFormField;
            }
            set 
            {
                this.MainFormField = value;
            }
        }

        public static ThreadEvents Instance 
        {
            get 
            {
                if (instance == null)
                    instance = new ThreadEvents();
                return instance;
            }
        }

        private void MyTimerTick(Object myObject,
                                            EventArgs myEventArgs)
        {

            API_Yedion.Instance.logger.Log_Method("Trying To Stop Background Worker...");
            API_Yedion.Instance.logger.Step_In();

            MyTimer.Stop();
            this.CancelAsync();
            this.Dispose();
            MyTimer = null;
            instance = null;

            API_Yedion.Instance.logger.Step_Out();
        }

        ThreadEvents()
        {
            WorkerReportsProgress = true;
            WorkerSupportsCancellation = true;
            MyTimer = new System.Windows.Forms.Timer();
            MyTimer.Interval = 5000;
            MyTimer.Tick += new EventHandler(MyTimerTick);

        }

        protected override void OnProgressChanged(ProgressChangedEventArgs e)
        {
            base.OnProgressChanged(e);

            MainForm.ProgressBarFile.Value = e.ProgressPercentage;
        }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            base.OnDoWork(e);

            while (Running)
            {
                switch (Command)
                {
                    case Operation.O_Login:
                        {
                            API_Yedion.Instance.Login(param1, param2);
                        }; break;

                    case Operation.O_Grades:
                        {
                            Result_rootSubject =  API_Yedion.Instance.Get_All_Grades(param1, param2, param3);
                        }; break;

                    case Operation.O_Notebooks:
                        {
                            Result_rootNotebook = API_Yedion.Instance.Get_All_Notebooks(param1, param2, param3);

                        }; break;

                    case Operation.O_Notebooks_Download:
                        {
                            API_Yedion.Instance.Download_Selected_Notebooks(Result_rootNotebook);
                            
                        }; break;
                }
                Running = false;
                this.CancelAsync();
                
            }
        }

        protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            base.OnRunWorkerCompleted(e);
            ReturnMethod();
        }

        public void RunCommand(Operation cmd,MyMethod Ret)
        {
            API_Yedion.Instance.logger.Log_Method(cmd.ToString());
            API_Yedion.Instance.logger.Step_In();
            ReturnMethod = Ret;
            if (CancellationPending)
            {
                API_Yedion.Instance.logger.WriteLn("CancellationPending.. Trying To Stop...");
                API_Yedion.Instance.logger.Step_Out();
                this.CancelAsync();
                return;
            }

            if (!this.IsBusy)
            {
                Running = true;
                Command = cmd;
                if (UseTimer)
                    MyTimer.Start();
                RunWorkerAsync();
            }
            API_Yedion.Instance.logger.Step_Out();
        }
    }

}
